from matplotlib.pyplot import *

rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
rcParams['axes.unicode_minus'] = False  # 解决符号无法显示


def DrawPath(Chrom, X):
    """
    画路径的函数
    :param Chrom: 待画路径
    :param X: 各城市的坐标位置
    :return: 随机解的路线的图像
    """
    figure()
    xlabel('横坐标')
    ylabel('纵坐标')
    title('轨迹图')
    Route = np.append(Chrom, Chrom[0])

    for i in range(0, len(Chrom)):
        scatter(X[int(Chrom[i]) - 1, 0], X[int(Chrom[i]) - 1, 1])
        text(X[int(Chrom[i]) - 1, 0] + 0.1, X[int(Chrom[i]) - 1, 1] + 0.1, str(i + 1))

    startx, starty = X[int(Chrom[0]) - 1, 0], X[int(Chrom[0]) - 1, 1]
    border = 0.5 * 0.87  # 0.05 *sqrt(3）/2
    plot([startx-border, startx+border, startx, startx-border], [starty-border, starty-border, starty+0.5, starty-border], 'r-')
    oldx, oldy = X[int(Route[0]) - 1, 0], X[int(Route[0]) - 1, 1]

    for i in range(1, len(Route)):
        newx, newy = X[int(Route[i]) - 1, 0], X[int(Route[i]) - 1, 1]
        arrow(oldx, oldy, newx - oldx, newy - oldy, length_includes_head=True,  # 增加的长度包含箭头部分
              head_width=0.2, head_length=0.3, fc='r', ec='b')
        oldx, oldy = newx, newy
    # show()
